package cc.web.vaaking.servlet.vacation;

import cc.web.vaaking.common.AjaxResult;
import cc.web.vaaking.common.AjaxTool;
import cc.web.vaaking.common.LoginContext;
import cc.web.vaaking.common.LoginUser;
import cc.web.vaaking.entity.SysAppLog;
import cc.web.vaaking.entity.SysVacationRecord;
import cc.web.vaaking.services.SysAppLogService;
import cc.web.vaaking.services.SysVacationRecordService;
import cc.web.vaaking.servlet.BaseHttpServlet;
import com.github.pagehelper.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/task/*")
public class TaskServlet extends BaseHttpServlet {
    SysVacationRecordService sysVacationRecordService = new SysVacationRecordService();
    SysAppLogService sysAppLogService = new SysAppLogService();

    @Override
    public void init() throws ServletException {
        routerMap.put("myTaskPage", this::myTaskPage);
        routerMap.put("historyTaskPage", this::historyTaskPage);
        routerMap.put("getMyTask", this::getMyTask);
        routerMap.put("getMyTaskNum", this::getMyTaskNum);
        routerMap.put("getHistoryTask", this::getHistoryTask);
    }

    private void getHistoryTask(HttpServletRequest request, HttpServletResponse response) {
        try {
            LoginUser loginUser = LoginContext.getCurrentUser();
            PageInfo<SysAppLog> info = new PageInfo<>(sysAppLogService.selectAllByUserId(loginUser.getId()));
            AjaxTool.returnAjax(response, AjaxResult.success(info));
        } catch (Exception e) {
            e.printStackTrace();
            AjaxTool.returnAjax(response, AjaxResult.error("失败"));
        }
    }

    private void historyTaskPage(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.getRequestDispatcher("../views/apply/historyTask.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void myTaskPage(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.getRequestDispatcher("../views/apply/myTask.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void getMyTaskNum(HttpServletRequest request, HttpServletResponse response) {
        try {
            LoginUser loginUser = LoginContext.getCurrentUser();
            int num = sysVacationRecordService.getStatusTaskNum(getRoleNum(loginUser.getRoleId()));
            AjaxTool.returnAjax(response, AjaxResult.success(num));
        } catch (Exception e) {
            e.printStackTrace();
            AjaxTool.returnAjax(response, AjaxResult.error("失败"));
        }
    }

    private void getMyTask(HttpServletRequest request, HttpServletResponse response) {
        try {
            LoginUser loginUser = LoginContext.getCurrentUser();
            List<SysVacationRecord> list = sysVacationRecordService.selectAllStatus(getRoleNum(loginUser.getRoleId()));
            PageInfo<SysVacationRecord> pageInfo = new PageInfo<>(list);
            AjaxTool.returnAjax(response, AjaxResult.success(pageInfo));
        } catch (Exception e) {
            e.printStackTrace();
            AjaxTool.returnAjax(response, AjaxResult.error("失败"));
        }

    }

    private String getRoleNum(int roleId) {
        switch (roleId) {
            case 1:
                return "1";
            case 2:
                return "0";
            default:
                return "-1";
        }
    }
}
